package com.honghu.villagepost.base;

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.honghu.villagepost.R;
import com.honghu.villagepost.activity.LoginActivity;
import com.honghu.villagepost.base.statusbar.ImmersionBar;
import com.honghu.villagepost.constant.Constants;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

/**
 * Created by mqwang on 2017/9/15.
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected String TAG = getClass().getSimpleName();

    protected TextView mMainTitleTv;
    private TextView mSubTitleTv;
    protected Toolbar mToolbar;
    protected Context mContext;
    private ProgressDialog dialog;

    /**
     * 是否允许沉浸式
     */
    private boolean mEnableStatusBar = true;

    private SystemBarTintManager tintManager;

    private ImmersionBar mImmersionBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = null;
        mContext = this;

        if (isNeedTitleBar()) {
            rootView = getLayoutInflater().inflate(R.layout.base_activity, null);

            LinearLayout rootLayout = (LinearLayout) rootView.findViewById(R.id.root_layout);
            View view = getLayoutInflater().inflate(getLayoutId(), null);
            rootLayout.addView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            mToolbar = (Toolbar) rootView.findViewById(R.id.common_toolbar);
            if (mToolbar != null) {
                mMainTitleTv = (TextView) rootView.findViewById(R.id.tv_title);
                mSubTitleTv = (TextView) rootView.findViewById(R.id.tv_sub_title);
                mToolbar.setNavigationIcon(R.mipmap.ic_back);
                mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });
                mSubTitleTv.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        onRightClickAction();
                    }
                });
            }
        } else {
            rootView = getLayoutInflater().inflate(getLayoutId(), null);
//            StatusBarUtil.setTranslucentDiff(this);//状态栏透明
//            StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);//状态栏透明

        }
        setContentView(rootView);
//        AutoUtils.auto(rootView);
        ButterKnife.bind(this);

        initStatusBar();

        if (isBindEventBus()) {//evenbus
            EventBus.getDefault().register(this);
        }

        init(savedInstanceState, rootView);

    }

    //Howard 2017-05-11
    protected View getContentView() {
        return this.findViewById(android.R.id.content);
    }

    /**
     * 是否需要标题栏
     */
    protected boolean isNeedTitleBar() {
        return true;
    }

    /**
     * 获取具体页面的layout id
     */
    protected abstract @LayoutRes
    int getLayoutId();

    /**
     * 初始化
     */
    protected abstract void init(Bundle savedInstanceState, View container);

    private void initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (mEnableStatusBar) {
                setTranslucentStatus(true);
            }
        }

        tintManager = new SystemBarTintManager(this);
        if (mEnableStatusBar) {
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setNavigationBarTintEnabled(false);
            tintManager.setStatusBarTintResource(R.color.colorPrimaryDark);

            initStatusBarTextColor();
        }
    }

    private void initStatusBarTextColor() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(android.R.color.transparent)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    public void setStatusBarTextColorWhite() {
        if (mImmersionBar != null) {
            mImmersionBar.statusBarDarkFont(false).init();
        }
    }

    public void setStatusBarTextColorDark() {
        if (mImmersionBar != null) {
            mImmersionBar.statusBarDarkFont(true, 0.2f).init();
        }
    }

    public void setStatusBarTintResource(int colorRes) {
        if (tintManager != null) {
            tintManager.setTintResource(colorRes);
//            tintManager.setStatusBarTintResource(colorRes);
        }
    }

    public void setStatusBarTintColor(int color) {
        if (tintManager != null) {
            tintManager.setTintColor(color);
//            tintManager.setStatusBarTintResource(colorRes);
        }
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 当前Activity是否需要绑定EventBus
     *
     * @return
     */
    protected boolean isBindEventBus() {
        return false;
    }

    /**
     * title右侧view的点击事件
     */
    protected void onRightClickAction() {

    }

    @Override
    public void onBackPressed() {
        if (!onBackEvent()) {
            try {
                super.onBackPressed();
            } catch (Throwable e) {
                finish();
            }
        }
    }

    protected boolean onBackEvent() {
        return false;
    }

    /**
     * 设置主标题
     */
    protected void setMainTitle(String mainTitle) {
        if (!TextUtils.isEmpty(mainTitle) && mToolbar != null) {
            mMainTitleTv.setText(mainTitle);
        }
    }

    /**
     * 设置主标题
     */
    protected void setSubTitle(String subTitle) {
        if (!TextUtils.isEmpty(subTitle) && mToolbar != null) {
            mSubTitleTv.setText(subTitle);
            mSubTitleTv.setVisibility(View.VISIBLE);
        } else {
            mSubTitleTv.setVisibility(View.GONE);
        }
    }

    /**
     * 设置副标题
     */
    protected void setMainSubTitle(String subTitle, int resId) {

        mSubTitleTv.setVisibility(View.VISIBLE);
        if (!ObjectUtils.isEmpty(subTitle)) {
            mSubTitleTv.setText(subTitle);
            mSubTitleTv.setCompoundDrawablesWithIntrinsicBounds(null, null,
                    null, null);
        } else if (resId > 0) {
            mSubTitleTv.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(resId), null,
                    null, null);
            mSubTitleTv.setText("");
        } else {
            mSubTitleTv.setVisibility(View.GONE);

        }

    }

    protected View getSubView() {
        return mSubTitleTv;
    }


    protected void showToast(String tip) {
        if (!TextUtils.isEmpty(tip)) {
            ToastUtils.showShort(tip);
        }
    }

    protected void showToast() {
        ToastUtils.showShort("操作成功");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        ToastUtil.hideToast();
        if (isBindEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    public Toolbar getmToolbar() {
        if (mToolbar != null) {
            return mToolbar;
        } else {
            return null;
        }
    }

    /**
     * 上拉下拉刷新完毕
     */
    public void finishRefresh(SmartRefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh(0);
            refreshLayout.finishLoadMore(0);
        }

    }

    public void showDialog(String msg) {
        dialog = new ProgressDialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage(msg);
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dissmissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public boolean isLogin() {
        return TextUtils.isEmpty(SPUtils.getInstance().getString(Constants.TOKEN)) ? false : true;
    }

    public boolean isLogin(boolean isGoLogin) {
        boolean isLogin = isLogin();
        if (!isLogin && isGoLogin) {
            Intent intent = new Intent(this, LoginActivity.class);
            intent.putExtra(Constants.NEED_GOBACK, true);
            startActivity(intent);
        }
        return isLogin;
    }

}
